<?php

namespace app\admin\controller;

use app\common\utils\JsonService;
use think\Exception;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\Response;
use Throwable;

/**
 * 后台异常处理
 *
 * Class AdminException
 * @package app\admin\controller
 */
class AdminException extends Handle
{

    public function render($request, Throwable $e): Response
    {
        // 参数验证错误
        if ($e instanceof ValidateException) {
            return JsonService::error($e->getError(),[],422);
        }
        if ($e instanceof \Exception && request()->isAjax()) {
            return JsonService::error($e->getMessage(),['code' => $e->getCode(), 'message' => $e->getMessage(), 'file' => $e->getFile()],$e->getCode());
        }

        return parent::render($request, $e);
    }
}